package com.example.abbitmqtest.controller;

import com.example.abbitmqtest.entity.Order;
import com.example.abbitmqtest.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 创建订单
     * @param order
     * @return
     */
    @PostMapping
    public ResponseEntity<Order> createOrder(@RequestBody Order order) {
        Order createdOrder = orderService.createOrder(order);
        return ResponseEntity.ok(createdOrder);
    }

    /**
     * 支付订单
     * @param orderNo
     * @return
     */
    @PostMapping("/{orderNo}/pay")
    public ResponseEntity<Boolean> payOrder(@PathVariable String orderNo) {
        boolean success = orderService.payOrder(orderNo);
        return ResponseEntity.ok(success);
    }

    /**
     * 取消订单
     * @param orderNo
     * @return
     */
    @PostMapping("/{orderNo}/cancel")
    public ResponseEntity<Boolean> cancelOrder(@PathVariable String orderNo) {
        boolean success = orderService.cancelOrder(orderNo);
        return ResponseEntity.ok(success);
    }

    /**
     * 根据订单号查询订单
     * @param orderNo
     * @return
     */
    @GetMapping("/{orderNo}")
    public ResponseEntity<Order> getOrder(@PathVariable String orderNo) {
        Order order = orderService.getOrderByOrderNo(orderNo);
        if (order != null) {
            return ResponseEntity.ok(order);
        }
        return ResponseEntity.notFound().build();
    }
}